package com.aw.guiguzi.adapter

import android.graphics.Typeface
import android.widget.LinearLayout
import android.widget.TextView
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.CalSuggestItemBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class MainCalSuggestAdapter :
    BaseQuickAdapter<CalSuggestItemBean, BaseViewHolder>(R.layout.adapter_main_cal_suggest) {
    private var mList: MutableList<CalSuggestItemBean>? = ArrayList()

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    override fun convert(helper: BaseViewHolder, bean: CalSuggestItemBean) {
        helper.setText(R.id.tv_type, bean.type)
            .setText(R.id.tv_name, bean.name + ": ")
            .setText(R.id.tv_value, bean.value)
        if (helper.layoutPosition != 0) {
            if (bean.type == mList!![helper.layoutPosition - 1].type) {
                helper.setVisible(R.id.tv_type, false)
            } else {
                helper.setVisible(R.id.tv_type, true)
            }
        } else {
            helper.setVisible(R.id.tv_type, true)
        }
        val ll_layout = helper.getView<LinearLayout>(R.id.ll_layout)

//        if(helper.getLayoutPosition()%2 == 0) {
//            ll_layout.setBackgroundColor(mContext.getResources().getColor(com.aw.baselib.R.color.color_item_1));
//        } else {
//            ll_layout.setBackgroundColor(mContext.getResources().getColor(com.aw.baselib.R.color.color_item_2));
//        }
        val tv_type = helper.getView<TextView>(R.id.tv_type)
        val tv_name = helper.getView<TextView>(R.id.tv_name)
        val tv_value = helper.getView<TextView>(R.id.tv_value)
        if (bean.isSuggestItem) {
            ll_layout.setBackgroundColor(mContext.resources.getColor(com.aw.baselib.R.color.color_app_green))
            helper.setTextColor(R.id.tv_type, mContext.resources.getColor(R.color.white))
                .setTextColor(R.id.tv_name, mContext.resources.getColor(R.color.white))
                .setTextColor(R.id.tv_value, mContext.resources.getColor(R.color.white))
            tv_type.setTypeface(null, Typeface.BOLD)
            tv_name.setTypeface(null, Typeface.BOLD)
            tv_value.setTypeface(null, Typeface.BOLD)
        } else {
            if (helper.layoutPosition % 2 == 0) {
                ll_layout.setBackgroundColor(mContext.resources.getColor(com.aw.baselib.R.color.color_item_1))
            } else {
                ll_layout.setBackgroundColor(mContext.resources.getColor(com.aw.baselib.R.color.color_item_2))
            }
            helper.setTextColor(R.id.tv_type, mContext.resources.getColor(R.color.black))
                .setTextColor(R.id.tv_value, mContext.resources.getColor(R.color.black))
            if (bean.name!!.contains("跳")) {
                helper.setTextColor(
                    R.id.tv_name,
                    mContext.resources.getColor(com.aw.baselib.R.color.red)
                )
            } else {
                helper.setTextColor(
                    R.id.tv_name,
                    mContext.resources.getColor(com.aw.baselib.R.color.black)
                )
            }
            tv_type.setTypeface(null, Typeface.NORMAL)
            tv_name.setTypeface(null, Typeface.NORMAL)
            tv_value.setTypeface(null, Typeface.NORMAL)
        }
    }

    fun addAllData(dataList: MutableList<CalSuggestItemBean>?) {
        mList = dataList
        setNewData(mList)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }
}